package tech.xs.common.framework.util;


import cn.hutool.core.io.IoUtil;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import tech.xs.common.framework.holder.SpringContextHolder;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;


/**
 * SpringBoot 工具
 *
 * @author 沈家文
 * @since 2023/5/10 14:03
 */
public class SpringUtil {

    private final static Logger log = LoggerFactory.getLogger(SpringUtil.class);

    /**
     * 获取资源文件内容列表
     *
     * @param path 资源路径
     * @return 内容列表
     */
    @SneakyThrows
    public static List<String> getResourceFileContentList(String path) {
        List<String> contentList = new ArrayList<>();
        try {
            Resource[] allResources = SpringContextHolder.getContext().getResources(path);
            for (Resource resource : allResources) {
                log.info("读取文件:" + resource);
                String content = IoUtil.readUtf8(resource.getInputStream());
                contentList.add(content);
            }
        } catch (FileNotFoundException e) {
            log.info("未查询到文件:" + path);
        }
        return contentList;
    }

}
